package com.mc.lp.security.user;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import com.mc.lp.view.constants.IConstants;
/**
 * @author: deepak.verma
 * @description: handle the request on successful login, it will decide landing page returned by AbstractRedirectStrategy based on user authorities.
 * */
public class SimpleAuthenticationSuccessHandler extends AbstractAuthenticationHandler implements AuthenticationSuccessHandler, IConstants {
	
	protected Logger logger = Logger.getLogger(SimpleAuthenticationSuccessHandler.class);
    public void onAuthenticationSuccess(HttpServletRequest request,
      HttpServletResponse response, Authentication authentication) throws IOException {
        handle(request, response, authentication);
        clearAuthenticationAttributes(request);
    }
 
    protected void handle(HttpServletRequest request,
      HttpServletResponse response, Authentication authentication) throws IOException {
        if (response.isCommitted()) {
            logger.debug("Response has already been committed. Unable to redirect :"+authentication.getAuthorities());
            return;
        }
        redirectStrategy.sendRedirectOnSuccess(request, response, authentication);
    }
}
